<?php
class ProductsController extends Zend_Controller_Action
{	
    public function init() {
         
    }
    public function indexAction()
    {
        $this->view->title = "Sản phẩm";
        $product                    = new Vina_Model_Products();
		$data = $product->fetchListToArray("1=1", "id desc");
        $paginator         = Zend_Paginator::factory($data);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(12);
            $paginator->setPageRange(6);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
           
            $this->view->data = $paginator;
    }
	public function categoryAction()
    {
      
        $category = new Vina_Model_Categories();

        $product                    = new Vina_Model_Products();
		$id                     =  $this->_request->getParam('id',0);
        $catInfo = $category->find($id);
        $catName = $catInfo->name;        
        $dataPro                = $product->fetchListToArray("category_id = $id", 'id desc');
      
            $paginator         = Zend_Paginator::factory($dataPro);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(12);
            $paginator->setPageRange(6);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
        
            $this->view->data = $paginator;
            $this->view->catName = $catName;
			$this->render('index');  
    }
    public function detailAction() {
        
        $product                    = new Vina_Model_Products();
        $id                     =  $this->_request->getParam('id',0);
        $data = $product->find($id)->toArray();
        $otherData = $product->fetchListToArray("id <> $id and category_id = {$data['category_id']}", "id desc", 3);
        $this->view->title = $data['name'];
		$this->view->data = $data;
        $this->view->otherData = $otherData;
        
    }
    public function filterAction() {
   
        $this->view->title = "Tìm kiếm nâng cao";
        $key                         =  $this->_request->getParam('key',false); 
     
       // echo $where;die;
        $product = new Vina_Model_Products();
        $data = $product->fetchListToArray("name like '%$key%'", 'id desc');
            $paginator         = Zend_Paginator::factory($data);
            $currentPage       = 1;
            //Check if the user is not on page 1
            $page              = $this->_getParam('page');
            if (! empty($page)) { //Where page is the current page
                $currentPage   = $this->_getParam('page');
            }
            //Set the properties for the pagination
            $paginator->setItemCountPerPage(12);
            $paginator->setPageRange(6);
            $paginator->setCurrentPageNumber($currentPage);
            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
            $paginator->setView($this->view);
           
            $this->view->data = $paginator;
			$this->render('index');
    }
    	
}
